<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\model\User;
use think\facade\Db;
use lizhili\Time;

class Index extends BaseController
{
    public function ajax()
    {
        $data=input('post.');
		if(!$data){
            return json(['code'=>400,'message'=>'提交数据错误！']);
		}
        $lizhili=BaseController::lizhili($data);
        if($lizhili['code']==0){
            return json(['code'=>10005,'message'=>'提交数据错误！']);
        }
        if($lizhili['code']===1){
            return json(['code'=>10001,'message'=>'提交超时，请重复提交！']);
        }
        if($lizhili['code']===2){
            return json(['code'=>10002,'message'=>'重复提交，请稍后重试！']);
        }
        if($lizhili['code']===4){
            return json(['code'=>10004,'shua_token'=>$lizhili['data'],'shua_time'=>$lizhili['shua_time']]);
        }
        $user_model=new User();
		
		if($data['type']=='get_index_show'){
			return json(['code'=>200,'message'=>'成功','data'=>'我是测试内容']);
		}
		
		
        //发送短信
        if ($data['type']=='get_login_yzm') {
            $msg=mt_rand(111111, 999999);
            $user=\model('user');
            if ($u=$user_model->where('phone', $data['phone'])->find()) {
                $user->where('id', $u['id'])->update([
                    'yzm'=>$msg
                ]);
            } else {
                $user->save([
                    'phone'=>$data['phone'],
                    'yzm'=>$msg,
                ]);
            }
            if ($this->sendsmg($data['phone'], $msg)) {
                return json(['code'=>200,'message'=>'成功','data'=>$msg]);
            } else {
                return json(['code'=>400,'message'=>'发送验证码失败']);
            }
        }
        //手机注册
        if ($data['type']=='phone_login') {
            $info=\json_decode($data['data'], true);
            $f_user=$user_model->where('user_no',$info['user_no'])->find();
            if(!$f_user){
                return json(['code'=>400,'message'=>'邀请码不正确']);
            }
            if(!$info['password']){
                return json(['code'=>400,'message'=>'请填写密码']);
            }
            if ($wo=$user_model->where('phone', $info['phoneno'])->where('yzm',$info['pwd'])->where('isopen',1)->find()) {
                if (!$wo['user_no']) {
                    $user_model->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                if (!$wo['fid']) {
                    $user_model->where('id', $wo['id'])->update(['fid'=>$f_user['id']]);
                }
                $token=md5(time()).$wo['id'];
                $user_model->where('id', $wo['id'])->update(['token'=>$token,'password'=>md5(sha1($info['password']))]);
                return json(['code'=>200,'message'=>'登陆成功','data'=>$token]);
            } else {
                return json(['code'=>400,'message'=>'手机或验证码不正确']);
            }
        }
        //手机找回密码
        if ($data['type']=='phone_login_zhao') {
            $info=\json_decode($data['data'], true);
            if(!$info['password']){
                return json(['code'=>400,'message'=>'请填写密码']);
            }
            if ($wo=$user_model->where('phone', $info['phoneno'])->where('yzm',$info['pwd'])->where('isopen',1)->find()) {
                if(!$wo['password']){
                    return json(['code'=>400,'message'=>'尚未注册，请注册后登陆']);
                }
                if (!$wo['user_no']) {
                    $user_model->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                $token=md5(time()).$wo['id'];
                $user_model->where('id', $wo['id'])->update(['token'=>$token,'password'=>md5(sha1($info['password']))]);
                return json(['code'=>200,'message'=>'登陆成功','data'=>$token]);
            } else {
                return json(['code'=>400,'message'=>'手机或验证码不正确']);
            }
        }
        //手机登陆
        if ($data['type']=='phone_login_login') {
            $info=\json_decode($data['data'], true);
            if ($wo=$user_model->where('phone', $info['phoneno'])->where('password',md5(sha1($info['password'])))->where('isopen',1)->find()) {
                if (!$wo['user_no']) {
                    return json(['code'=>400,'message'=>'未注册']);
                }
                $token=md5(time()).$wo['id'];
                $user_model->where('id', $wo['id'])->update(['token'=>$token]);
                return json(['code'=>200,'message'=>'登陆成功','data'=>$token]);
            } else {
                return json(['code'=>400,'message'=>'手机或密码不正确']);
            }
        }
        //说明列表
        if ($data['type']=='get_xiangguan_list') {
            $res=Db::name('shuoming')->whereNull('delete_time')->where('isopen',1)->select();
            if($res){
                return json(['code'=>200,'message'=>'成功','data'=>$res]);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        //获取相关
        if ($data['type']=='get_xiangguan') {
            $arr1=explode('_',$data['lei']);
            if($arr1[0]=='tongzhi'){
                $res=Db::name('article')->whereNull('delete_time')->where('isopen',1)->find($arr1[1]);
            }else{
                $res=Db::name('shuoming')->whereNull('delete_time')->where('isopen',1)->find($data['lei']);
            }
            if($res){
                return json(['code'=>200,'message'=>'成功','data'=>$res]);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        //app  升级
        if ($data['type']=='update_app') {
            $config=Db::name('mall_update')->where('isopen',1)->order('app_v desc')->find();
            if($config){
                return json(['code'=>200,'app_v'=>$config['app_v'],'url'=>'http://'.\request()->host().'/update/update_'.$config['app_v'].'.apk','desc'=>$config['desc']]);
            }
            return json(['code'=>400]);
        }
        //查看是否有权限
        if ($data['type']=='get_my_check') {
            if($user=$user_model->where('token',$data['token'])->where('isopen',1)->find()){
                return json(['code'=>200,'message'=>'成功']);
            }else{
                return json(['code'=>400,'message'=>'失败']);
            }
        }
        //微信登陆
        if ($data['type']=='get_login_wx_user') {
            $raw=\json_decode($data['rawData'],true);
            $appid=config('wx_config')['appid'];
            $secret=config('wx_config')['secret'];
            $code=$this->curl('https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$data['code'].'&grant_type=authorization_code');
            if($code and $raw){
                $raw['openid']=$code['openid'];
                if($user=$user_model->where('openid_wx',$code['openid'])->find()){
                    $token=md5($user['openid_wx'].time());
                    $user_model->where('id',$user['id'])->update(['token'=>$token,'openid_wx'=>$code['openid']]);
                    if(!$user['fid'] and $data['fid']){
                        $user_model->where('id',$user['id'])->update(['fid'=>$data['fid']]);
                    }
                    return json(['code'=>200,'message'=>'已经绑定微信','token'=>$token,'uid'=>$user['id']]);
                }else{
                    $raw['token']=md5($raw['openid'].time());
                    $linshi=\model('user');
                    $raw['openid_wx']=$raw['openid'];
                    $raw['unionid']=$code['unionid'] ?: '';
                    $linshi->save($raw);
                    if($data['fid']){
                        $user_model->where('id',$linshi->id)->update(['fid'=>$data['fid']]);
                    }
                    $user_model->where('id',$linshi->id)->update(['user_no'=>100000+$linshi->id]);
                    return json(['code'=>200,'message'=>'已经绑定微信','token'=>$raw['token'],'uid'=>$linshi->id]);
                }
            }else{
                return json(['code'=>400,'message'=>'登陆失败']);
            }
        }
        //微信登陆 app登陆
        if ($data['type']=='get_login_wx_app') {
            $info=\json_decode($data['info'],true);
            if($info['unionId']){
                if($user=$user_model->where('unionId',$info['unionId'])->find()){
                    $token=md5($user['openid_app'].time());
                    $user_model->where('id',$user['id'])->update(['token'=>$token,'openid_app'=>$info['openId']]);
                    if(!$user['fid'] and $data['fid']){
                        $user_model->where('id',$user['id'])->update(['fid'=>$data['fid']]);
                    }
                    return json(['code'=>200,'message'=>'已经绑定微信','token'=>$token,'uid'=>$user['id']]);
                }else{
                    $raw=[];
                    $raw['token']=md5($info['openId'].time());
                    $linshi=\model('user');
                    $raw['openid_app']=$info['openId'];
                    $raw['nickName']=$info['nickName'];
                    $raw['gender']=$info['gender'];
                    $raw['city']=$info['city'];
                    $raw['province']=$info['province'];
                    $raw['country']=$info['country'];
                    $raw['avatarUrl']=$info['avatarUrl'];
                    $raw['unionId']=$info['unionId'];
                    $linshi->save($raw);
                    if($data['fid']){
                        $user_model->where('id',$linshi->id)->update(['fid'=>$data['fid']]);
                    }
                    $user_model->where('id',$linshi->id)->update(['user_no'=>100000+$linshi->id]);
                    return json(['code'=>200,'message'=>'已经绑定微信','token'=>$raw['token'],'uid'=>$linshi->id]);
                }
            }else{
                return json(['code'=>400,'message'=>'登陆失败']);
            }
        }
        return json(['code'=>400,'message'=>'非法获取']);
    }

}
